/*	
 *				
 *					ISNW ---- It is a war.It is not warcraft!
 *
 *
 *					Author:					zhoukeli(haxixi_keli)
 *					Filename:				BillboardSceneNode.h
 *					CreateTime:				2008/12/29
 *					ModifyTime:				2008/12/29
 *
 */

#ifndef __ISNW_BILLBOARD_SCENE_NODE_H__
#define __ISNW_BILLBOARD_SCENE_NODE_H__

#include "include/IBillboardSceneNode.h"
#include "include/isnwMaterial.h"
#include "include/isnwVertex.h"
#include "include/ICameraSceneNode.h"

namespace ISNW
{
namespace scene
{
	class BillboardSceneNode : public IBillboardSceneNode
	{
	public:
		BillboardSceneNode(ISceneManager* sm,ISceneNode* parent,s32 id,core::dimention2df size,core::vector3df pos,
			core::vector3df rotate,core::vector3df scale)
			:IBillboardSceneNode(sm,parent,id,size,pos,rotate,scale)
			,dimention(size)
		{
			indices_buffer[0] = 0;
			indices_buffer[1] = 1;
			indices_buffer[2] = 2;

			indices_buffer[3] = 0;
			indices_buffer[4] = 2;
			indices_buffer[5] = 3;

			updateVertexBuffer();
		}

	public:
		void setsize(core::dimention2df size)
		{
			dimention = size;
		}

		core::dimention2df getsize() const
		{
			return dimention;
		}

		void render();

		void onRegisterSceneNode();

		void setmaterial(video::material m)
		{
			Material = m;
		}

		video::material getmaterial() const
		{
			return Material;
		}

		void updateVertexBuffer()
		{
			if(scenemanager && scenemanager->getActiveCamera())
			{
				core::vector3df position = getAbsolutePosition();

				core::vector3df camera_postion = scenemanager->getActiveCamera()->getAbsolutePosition();

				core::vector3df line_from_camera = position - camera_postion;

				if(line_from_camera == core::vector3df(0,0,0))
				{
					line_from_camera.z = -1;
				}

				core::vector3df upvec = scenemanager->getActiveCamera()->getUpVec();

				core::vector3df horizon = upvec.crossproduct(line_from_camera);

				horizon.normalize();

				core::vector3df vertical = line_from_camera.crossproduct(horizon);

				vertical.normalize();

				vertex_buffer[0].pos = position + dimention.width * 0.5f * horizon + dimention.height * 0.5f * vertical;
				vertex_buffer[1].pos = position + dimention.width * 0.5f * horizon - dimention.height * 0.5f * vertical;
				vertex_buffer[2].pos = position - dimention.width * 0.5f * horizon - dimention.height * 0.5f * vertical;
				vertex_buffer[3].pos = position - dimention.width * 0.5f * horizon + dimention.height * 0.5f * vertical;

				vertex_buffer[0].coords = core::vector2df(0,0);
				vertex_buffer[1].coords = core::vector2df(1,0);
				vertex_buffer[2].coords = core::vector2df(1,1);
				vertex_buffer[3].coords = core::vector2df(0,1);

				vertex_buffer[0].normal = line_from_camera;
				vertex_buffer[1].normal = line_from_camera;
				vertex_buffer[2].normal = line_from_camera;
				vertex_buffer[3].normal = line_from_camera;
			}
		}

	protected:
		core::dimention2df dimention;

		video::material	Material;

		video::vertex vertex_buffer[4];
		u16 indices_buffer[6];
		
	};
} // end namespace scene
} // end namespace ISNW
#endif